package com.nulstudio.sppm.controller;

import com.nulstudio.sppm.common.NulResult;
import com.nulstudio.sppm.domain.dto.WarehouseDto;
import com.nulstudio.sppm.domain.vo.WarehouseVo;
import com.nulstudio.sppm.service.WarehouseService;
import jakarta.annotation.Resource;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/warehouse")
public class WarehouseController {

    @Resource
    @NonNull
    private WarehouseService warehouseService;

    @GetMapping
    @NonNull
    public NulResult<NulResult.PageInfoResult<WarehouseVo>> all(
            @RequestParam(name = "name", defaultValue = "") @NonNull String name,
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "pageSize", defaultValue = "100000") int pageSize
    ) {
        return NulResult.response(warehouseService.fetchAll(name, page, pageSize));
    }

    @PostMapping
    @NonNull
    public NulResult<Void> create(@RequestBody @NonNull WarehouseDto warehouseDto) {
        warehouseService.create(warehouseDto.name(), warehouseDto.type(), warehouseDto.address());
        return NulResult.response();
    }

    @PutMapping("/{id}")
    @NonNull
    public NulResult<Void> update(@PathVariable int id, @RequestBody @NonNull WarehouseDto warehouseDto) {
        warehouseService.update(id, warehouseDto.name(), warehouseDto.type(), warehouseDto.address());
        return NulResult.response();
    }

    @DeleteMapping("/{id}")
    @NonNull
    public NulResult<Void> delete(@PathVariable int id) {
        warehouseService.delete(id);
        return NulResult.response();
    }

    @GetMapping("/type")
    @NonNull
    public NulResult<List<String>> getTypeList() {
        return NulResult.response(warehouseService.fetchTypeList());
    }

}
